package com.web.test.service.impl;

import com.web.test.mapper.UserMapper;
import com.web.test.pojo.User;
import com.web.test.pojo.UserExample;
import com.web.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //登录业务
    @Override
    public User login(String username, String password) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andUserpasswordEqualTo(password);
        List<User> list=userMapper.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    //注册业务中的添加用户至数据库方法
    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    //删除用户
    @Override
    public void deleteUser(Integer userid){
        userMapper.deleteByPrimaryKey(userid);
    }

    //注册业务中检查用户名是否存在
    @Override
    public boolean checkUserExist(String username) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> list=userMapper.selectByExample(example);
        if(list!=null && list.size()>0){
            return true;
        }
        return false;
    }

    //查询数据库表中所有数据并返回
    @Override
    public List<User> queryUsers() {
        return userMapper.selectByExample(null);
    }


}
